function adaper() {
	navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
	window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;

	if(navigator.mediaDevices === undefined) {
		navigator.mediaDevices = {};
	}
	// Some browsers partially implement mediaDevices. We can't just assign an object
	// with getUserMedia as it would overwrite existing properties.
	// Here, we will just add the getUserMedia property if it's missing.
	if(navigator.mediaDevices.getUserMedia === undefined) {
		navigator.mediaDevices.getUserMedia = function(constraints) {

			// First get ahold of the legacy getUserMedia, if present
			var getUserMedia = (navigator.getUserMedia ||
				navigator.webkitGetUserMedia ||
				navigator.mozGetUserMedia);

			// Some browsers just don't implement it - return a rejected promise with an error
			// to keep a consistent interface
			if(!getUserMedia) {
				return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
			}

			// Otherwise, wrap the call to the old navigator.getUserMedia with a Promise
			return new Promise(function(resolve, reject) {
				getUserMedia.call(navigator, constraints, resolve, reject);
			});
		}
	}
}

function convertImgToImgData(url, callback, outputFormat) {
	var canvas = document.createElement('CANVAS'),
		ctx = canvas.getContext('2d'),
		img = new Image;
	img.crossOrigin = 'Anonymous';
	img.src = url;
	img.onload = function() {
		canvas.height = img.height;
		canvas.width = img.width;
		ctx.drawImage(img, 0, 0);
		var imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
		callback.call(this, imgData, img.width, img.height);
		canvas = null;
	};
}

function getDescriptors(imageData) {
	var gray = tracking.Image.grayscale(tracking.Image.blur(imageData.data, imageData.width, imageData.height, blurRadius), imageData.width, imageData.height);
	var corners = tracking.Fast.findCorners(gray, imageData.width, imageData.height);
	var descriptors = tracking.Brief.getDescriptors(gray, imageData.width, corners);
	return {
		corners: corners,
		descriptors: descriptors
	};
}

function initMatch() {
	window.descriptorLength = 256;
	window.matchesShown = 30;
	window.blurRadius = 3;
	tracking.Brief.N = window.descriptorLength;
}

//function doMatch(imageData1, imageData2) {
//	window.descriptorLength = 256;
//	window.matchesShown = 30;
//	window.blurRadius = 3;
//	tracking.Brief.N = window.descriptorLength;
//
//	var gray1 = tracking.Image.grayscale(tracking.Image.blur(imageData1.data, imageData1.width, imageData1.height, blurRadius), imageData1.width, imageData1.height);
//	var gray2 = tracking.Image.grayscale(tracking.Image.blur(imageData2.data, imageData2.width, imageData2.height, blurRadius), imageData2.width, imageData2.height);
//
//	var corners1 = tracking.Fast.findCorners(gray1, imageData1.width, imageData1.height);
//	var corners2 = tracking.Fast.findCorners(gray2, imageData2.width, imageData2.height);
//
//	var descriptors1 = tracking.Brief.getDescriptors(gray1, imageData1.width, corners1);
//	var descriptors2 = tracking.Brief.getDescriptors(gray2, imageData2.width, corners2);
//
//	var matches = tracking.Brief.reciprocalMatch(corners1, descriptors1, corners2, descriptors2);
//	if(matches.length < 7) return 10000;
//	matches.sort(function(a, b) {
//		return b.confidence - a.confidence;
//	});
//
//	var angles = [];
//	for(var i = 0; i < Math.min(window.matchesShown, matches.length); i++) {
//		var a = matches[i].keypoint1[0] / matches[i].keypoint1[1];
//		var b = matches[i].keypoint2[0] / matches[i].keypoint2[1];
//
//		angles[i] = a - b;
//		//		console.log("i " + i);
//		//		console.log("a " + a);
//		//		console.log("b " + b);
//	}
//
//	angles.sort(function(a, b) {
//		return b - a;
//	});
//
//	var mid = angles[Math.floor(angles.length / 2)];
//	var total = 0;
//	for(var i = 0; i < angles.length; i++) {
//		total += (angles[i] - mid);
//	}
//	var deviation = total / angles.length;
//	console.log("deviation " + deviation);
//	return deviation;
//};

function doMatch(d1, d2) {
	var matches = tracking.Brief.reciprocalMatch(d1.corners, d1.descriptors, d2.corners, d2.descriptors);
	if(matches.length < 7) return 10000;
	matches.sort(function(a, b) {
		return b.confidence - a.confidence;
	});

	var angles = [];
	for(var i = 0; i < Math.min(window.matchesShown, matches.length); i++) {
		var a = matches[i].keypoint1[0] / matches[i].keypoint1[1];
		var b = matches[i].keypoint2[0] / matches[i].keypoint2[1];

		angles[i] = a - b;
		//		console.log("i " + i);
		//		console.log("a " + a);
		//		console.log("b " + b);
	}

	angles.sort(function(a, b) {
		return b - a;
	});

	var mid = angles[Math.floor(angles.length / 2)];
	var total = 0;
	for(var i = 0; i < angles.length; i++) {
		total += (angles[i] - mid);
	}
	var deviation = total / angles.length;
	console.log("deviation " + deviation);
	return deviation;
};

window.log = function(str) {
	var prelog = document.getElementById("prelog");
	if(prelog) {
		prelog.innerHTML += str;
		prelog.innerHTML += '\n';
	}
}

window.print = function(str) {
	var predata = document.getElementById("predata");
	if(predata) {
		predata.innerHTML = str;
	}
}